<template>
  <div class="login-container" v-loading="loading" element-loading-text="拼命加载中">
    <div class="box_div">
      <div class="left">
        <h2 class="box_h2">Welcome to</h2>
        <h2 class="box_h2">Tianhai Space Positioning System</h2>
        <!-- <p>数据采集 | 存储计算 | 工艺组态 | 智慧大屏</p> -->
        <p>数据采集 | 存储计算 | 工艺组态 </p>
        <p>工业应用一体化物联网平台</p>
      </div>
      <div class="right">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
          label-position="left">
          <div class="title-container">
            <h2 class="title">用户登录</h2>
            <p>Hello!欢迎登录后台管理系统</p>
          </div>

          <el-form-item prop="username" style="border: 1px solid #dcdfe6;">
            <span class="svg-container">
              <!-- <svg-icon icon-class="user" /> -->
              <i class="el-icon-user" style="font-size: 15px;" />
            </span>
            <el-input ref="username" v-model="loginForm.username" placeholder="账号" name="username" type="text"
              tabindex="1" size="mini" auto-complete="on" />
          </el-form-item>

          <el-form-item prop="password" style="border: 1px solid #dcdfe6;">
            <span class="svg-container">
              <!-- <svg-icon icon-class="password" /> -->
              <i class="el-icon-lock" style="font-size: 15px;" />
            </span>
            <el-input :key="passwordType" size="mini" ref="password" v-model.trim="loginForm.password"
              :type="passwordType" placeholder="密码" name="password" tabindex="2" auto-complete="on" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
          <!-- <Code @codeEnter="codeEnter" /> -->
          <!-- <el-form-item prop="password">
                        <span class="svg-container">
                            <svg-icon icon-class="guide" />
                        </span>
                        <el-input
                            :key="passwordType"
                            size="mini"
                            ref="code"
                            v-model="loginForm.code"
                            placeholder="验证码"
                            name="password"
                            tabindex="2"
                            auto-complete="on"
                            @keyup.enter.native="handleLogin"
                        />
                        <span class="show-pwd" @click="showPwd">
                          <svg-icon icon-class="" />
                        </span>
                    </el-form-item> -->
          <p class="yzm">
            <!-- <span @click="$router.push('/codeLogin')"> 手机验证码登录 </span> -->
            <!-- <span> 手机验证码登录 </span> -->
          </p>

          <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px;margin-top: 20px;"
            @click.native.prevent="handleLogin">登录</el-button>
          <el-button plain @click="$router.push('/register')"
            style="width: 100%; margin-bottom: 30px; margin-left: 0">注册</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  import { login } from "@/api/user";
  import { ADMINUSERNAME, ADMINUSERPASSWORD, USERNAME, USERPASSWORD, SHUTONGADMINUSERNAME, SHUTONGADMINPASSWORD } from "@/data/user";
  // import Code from "@/components/Code/index.vue";

  export default {
    name: "Login",
    components: {
      // Code,
    },
    data() {
      return {
        isLoginShow: false,
        loginForm: {
          username: "",
          // username: "天海科技",
          password: "",
        },
        loginRules: {
          username: [
            {
              required: true,
              trigger: "blur",
              message: '请输入账号'
            },
          ],
          password: [
            {
              required: true,
              trigger: "blur",
              message: '请输入密码'
            },
          ],
        },
        loading: false,
        passwordType: "password",
        redirect: undefined,
      };
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true,
      },
    },
    mounted() {
      localStorage.setItem("token", "");
      // 绑定监听事件
      window.addEventListener("keydown", this.handleEnterClick);
    },
    destroyed() {
      // 销毁事件
      window.removeEventListener("keydown", this.handleEnterClick, false);
    },
    methods: {
      async login() {
        if (this.loginForm.username == ADMINUSERNAME && this.loginForm.password == ADMINUSERPASSWORD) {
          this.$message.success("登录成功");
          localStorage.setItem("token", "token");
          // 用户权限
          localStorage.setItem("userType", "admin");
          this.$router.push({ path: this.redirect || '/' });
          this.loading = false;
        } else if (this.loginForm.username == SHUTONGADMINUSERNAME && this.loginForm.password == SHUTONGADMINPASSWORD) {
          this.$message.success("登录成功");
          localStorage.setItem("token", "token");
          // 用户权限
          localStorage.setItem("userType", "admin_plus");
          this.$router.push({ path: this.redirect || '/' });
          this.loading = false;
        } else if (this.loginForm.username == USERNAME && this.loginForm.password == USERPASSWORD) {
          this.$message.success("登录成功");
          localStorage.setItem("token", "token");
          // 用户权限
          localStorage.setItem("userType", "ordinary");
          this.$router.push({ path: this.redirect || '/' });
          this.loading = false;
        } else {
          try {
            const data = {
              username: this.loginForm.username,
              password: this.loginForm.password
            };
            const { code, result } = await login(data);
            if (code == 0) {
              localStorage.setItem("userType", "ordinary");
              this.$message.success(result);
              localStorage.setItem("token", "token");
              this.$router.push({ path: this.redirect || '/' });
              this.loading = false;
            }
          } catch (error) {
            this.loading = false;
            console.log(error);
          }
        }
      },
      codeEnter(code) {
        this.isLoginShow = true;
      },
      showPwd() {
        if (this.passwordType === "password") {
          this.passwordType = "";
        } else {
          this.passwordType = "password";
        }
        this.$nextTick(() => {
          this.$refs.password.focus();
        });
      },
      handleEnterClick(e) {
        // 回车则执行登录方法 enter键的ASCII是13
        if (e.keyCode == 13 || e.keyCode == 32) {
          this.handleLogin();
        } // 定义的登录方法
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          this.login();
        });

        // this.$router.push({ path: this.redirect || '/' })

        // this.$refs.loginForm.validate(valid => {
        //   if (valid) {
        //     this.loading = true
        //     this.$store.dispatch('user/login', this.loginForm).then(() => {
        //       this.$router.push({ path: this.redirect || '/' })
        //       this.loading = false
        //     }).catch(() => {
        //       this.loading = false
        //     })
        //   } else {
        //     console.log('error submit!!')
        //     return false
        //   }
        // })
      }
    },
  };
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg: #fff;
  $light_gray: #fff;
  $cursor: #000;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {
    .el-form-item__content {
      height: 40px;
      line-height: 26px;
    }

    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: #000;
        height: 40px;
        caret-color: #000;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;

  .login-container {
    min-height: 100%;
    width: 100%;
    // background-color: $bg;
    // background: url(../../assets/img/login1.jpg);
    background: url(../../assets/img/logo_box.jpg);
    background-repeat: no-repeat;
    background-size: cover;
    overflow: hidden;

    .yzm {
      text-align: right;
      padding: 0px;
      cursor: pointer;
      margin-bottom: 20px;
      font-size: 14px;
      margin-top: -10px;
      color: rgba(128, 128, 128, 1);
      user-select: none;
    }

    &>div {
      // width: 1128px;
      // height: 695px;
      width: 57%;
      height: 70%;
      opacity: 1;
      border-radius: 8px;
      background: rgba(255, 255, 255, 1);
      // box-shadow: 0px 10px 20px 0px rgba(219, 231, 238, 1);
      position: fixed;
      left: 50%;
      // top: 50%;
      top: 53%;
      transform: translateX(-50%) translateY(-50%);
      box-sizing: border-box;
      display: flex;

      .left {
        width: 432px;
        height: 100%;
        background-color: rgba(24, 144, 255, 1);
        text-align: center;
        padding-top: 112px;
        box-sizing: border-box;
        color: #fff;

        &>h2 {
          margin-top: 0;
          font-size: 30px;
        }

        &>p {
          font-size: 16px;
        }
      }
    }

    .login-form {
      position: relative;
      // width: 520px;
      width: 554px;
      max-width: 100%;
      padding: 160px 35px 0 132px;
      box-sizing: border-box;
      margin: 0 auto;
      overflow: hidden;
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 20px;
        color: rgba(0, 0, 0, 1);
        margin: 0px auto 40px auto;
        font-weight: bold;
        margin-bottom: 0;
      }

      &>p {
        margin-top: 8px;
        color: rgba(128, 128, 128, 1);
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }

  @media screen and (max-width: 1740px) {
    .box_h2 {
      font-size: 24px !important;
    }
  }

  @media screen and (max-height: 898px) {
    .login-container .login-form {
      padding: 98px 35px 0px 132px !important;
    }
  }

  @media screen and (max-width: 1521px) {
    .login-container>div {
      width: 935px !important;
      height: 559px !important;
    }
  }
</style>